package com.att.action;

import com.adkfp.utils.LayuiUtil;
import com.att.bean.UserOfRunBean;
import com.att.bean.UserOfRunDeilBean;
import com.att.service.UserOfRunDeilService;
import com.att.service.UserOfRunService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("userOfRun")
public class UserOfRunAction {

    @Resource(name = "userOfRunService")
    private UserOfRunService userOfRunService;
    @Resource(name = "userOfRunDeilService")
    private UserOfRunDeilService userOfRunDeilService;

    /**
     * Description: 转发到页面
     */
    @RequestMapping("userOfRunIndex")
    public String userOfRunIndex() {
        return "settings/userOfRun";
    }

    /**
     * Description: 带条件查询出勤记录
     * @param bean
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll(UserOfRunBean bean) {
        List<UserOfRunBean> list = userOfRunService.findByCondition(bean);
        long count = userOfRunService.findByConditionCount(bean);
        return LayuiUtil.data(count, list);
    }


    /**
     * Description: 查询未排班人员统计
     * @param bean
     */
    @RequestMapping("findNoUserOfRun")
    @ResponseBody
    public Object findNoUserOfRun(UserOfRunBean bean) {
        List<UserOfRunBean> arrayList = new ArrayList<>();
        List<UserOfRunBean> list = userOfRunService.findNoUserOfRunCondition(bean);
        for (int i = 0;i<list.size();i++){
            if(list.get(i).getNumRunId() == null){
                arrayList.add(list.get(i));
            }
        }
        long count = userOfRunService.findNoUserOfRunCount(bean);
        return LayuiUtil.data(count, arrayList);
    }

    //新增
    @RequestMapping("insert")
    @ResponseBody
    public String insert(UserOfRunBean bean) {
        boolean flag = userOfRunService.insert(bean);
        if(flag){
            return "SUCCESS";
        }
        return "ERROR";
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public String delete(UserOfRunBean bean) {
        boolean flag = userOfRunService.delete(bean);
        if(flag){
            UserOfRunDeilBean user = new UserOfRunDeilBean();
            user.setUserId(bean.getUserId());
            user.setNumRunId(bean.getNumRunId());
            boolean flag2 = userOfRunDeilService.delete(user);
            if(!flag2){
                return "ERROR";
            }
        }
        return "SUCCESS";
    }
}
